﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using System.Text;
using System.Text.RegularExpressions;

public partial class Manage_Admin_SiteRole : System.Web.UI.Page
{
    private KeRui.BLL.AdminFlag bllAdminFlag = new KeRui.BLL.AdminFlag();

    protected KeRui.Model.CmsSite modelCmsSite = new KeRui.Model.CmsSite();
    protected KeRui.Model.AdminRole modelAdminRole = new KeRui.Model.AdminRole();
    private KeRui.BLL.AdminRole bllAdminRole = new KeRui.BLL.AdminRole();
    protected int siteID = -1;
    protected int roleID = -1;
    private ManageErrMsg Err = new ManageErrMsg();
    protected void Page_Load(object sender, EventArgs e)
    {
        siteID = PageRequest.GetInt("siteID", -1);
        roleID = PageRequest.GetInt("roleID", -1);
        KeRui.BLL.CmsSite bllCmsSite = new KeRui.BLL.CmsSite();
        modelCmsSite = bllCmsSite.GetModel(siteID);
        if (modelCmsSite == null)
        {
            Err.AddErr("Error:siteID没有找到。"); Err.ChkErr();
        }
        modelAdminRole = bllAdminRole.GetModel(roleID);
        if (modelAdminRole == null)
        {
            Err.AddErr("Error:roleID没有找到。"); Err.ChkErr();
        }
        switch (ManageHelper.PageAct())
        {
            case "save":
                Save();
                break;
            default:
                break;
        }
    }

    #region 保存
    private void Save()
    {
        string SiteRoleFlag = PageRequest.GetString("RoleFlag");
        if (SiteRoleFlag == "")
        {
            SiteRoleFlag = "site_" + siteID;
        }
        else
        {
            SiteRoleFlag = "site_" + siteID + "," + SiteRoleFlag;//多了这部
        }

        KeRui.Model.AdminRole modelAdminRole_old = bllAdminRole.GetModel(roleID);//获取原来的modelAdminRole_old
        string pattern = @"site\w*?_"+siteID+"";
        Regex regex = new Regex(pattern);
        MatchCollection collection = regex.Matches(modelAdminRole.RoleFlag);
        foreach (Match match in collection)
        {
            //把以前该网站的权限全部替换成空字符
            modelAdminRole_old.RoleFlag = modelAdminRole_old.RoleFlag.Replace((match.Captures[0]).ToString(), "").Replace(",,", ","); 
        }
        if (modelAdminRole_old.RoleFlag.IndexOf(',') == 0)
        {
            modelAdminRole_old.RoleFlag = modelAdminRole_old.RoleFlag.Substring(1);
        }
        else if (modelAdminRole_old.RoleFlag.LastIndexOf(',') == modelAdminRole_old.RoleFlag.Length - 1 && modelAdminRole_old.RoleFlag.Length>0)
        {
            modelAdminRole_old.RoleFlag = modelAdminRole_old.RoleFlag.Substring(0, modelAdminRole_old.RoleFlag.Length-1);
        }
        modelAdminRole.RoleID = roleID;
        if (modelAdminRole_old.RoleFlag == "")
        {
            modelAdminRole.RoleFlag = SiteRoleFlag;
        }
        else
        {
            modelAdminRole.RoleFlag = modelAdminRole_old.RoleFlag + "," + SiteRoleFlag;
        }
        bllAdminRole.UpdateRoleFlagForSiteFlag(modelAdminRole);
        Response.Redirect("SiteRole.aspx?siteid="+siteID+"&roleid="+roleID+"&sucmsg=保存成功。");
    }
    #endregion

    #region 权限列表
    int EmptyNum = 0;
    protected string GetRoleFlagList(string roleFlag, int siteID)
    {
        roleFlag = "," + roleFlag + ",";
        StringBuilder xs = new StringBuilder();
        DataTable dt = bllAdminFlag.GetRoleChildFlag(0, 1).Tables[0];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            string checkflag = "";
            if (roleFlag.IndexOf("," + dt.Rows[i][4].ToString() + "_" + siteID + ",") != -1)
            {
                checkflag = "checked";
            }
            EmptyNum = 0;
            xs.AppendFormat("<div id=\"p{0}\"><ul class=\"ultitle\" id=\"t{0}\"><input type=\"checkbox\" name=\"RoleFlag\" id=\"{0}\"  value=\"{3}\" " + checkflag + " onclick=\"selAll(this.id)\">" +
                "<img align=\"absmiddle\" src=\"/Manage/pic/menu/{2}\"/>&nbsp;<span id=\"s{0}\"onclick=\"showchildnode(this.id)\" style=\"cursor:pointer;\">{1}</span></ul>",
                dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString(), dt.Rows[i][3].ToString(), dt.Rows[i][4].ToString() + "_" + siteID);
            xs.Append(GetChildRoleFlagList(Convert.ToInt32(dt.Rows[i][0]), roleFlag, siteID));
            xs.Append("</div>");
        }
        return xs.ToString();
    }
    private string GetChildRoleFlagList(int pID, string roleFlag, int siteID)
    {
        roleFlag = "," + roleFlag + ",";
        string xs = "";
        for (int n = 0; n < EmptyNum; n++)
        {
            xs += "　";
        }
        StringBuilder strRFlaglist = new StringBuilder();
        if (EmptyNum == 0)
        {
            strRFlaglist.Append("<ul class=\"divurl\" id=\"c" + pID + "\" style=\"border:1px solid #99CCFF;\">");
        }
        else
        {
            strRFlaglist.Append("<ul class=\"divurl\" id=\"c" + pID + "\">");
        }
        DataTable dtChild = bllAdminFlag.GetRoleChildFlag(pID, 1).Tables[0];
        for (int m = 0; m < dtChild.Rows.Count; m++)
        {
            string checkflag = "";
            if (roleFlag.IndexOf("," + dtChild.Rows[m][4].ToString() + "_" + siteID + ",") != -1)
            {
                checkflag = "checked";
            }
            strRFlaglist.AppendFormat("<li>" + xs + "<input type=\"checkbox\"  name=\"RoleFlag\" id=\"{0}\" value=\"{3}\" " + checkflag + " onclick=\"conclick(this.id," + pID + ")\">" +
                "<img align=\"absmiddle\" src=\"/Manage/pic/menu/{2}\"/>&nbsp;{1}</li>", dtChild.Rows[m][0].ToString(), dtChild.Rows[m][1].ToString(),
                dtChild.Rows[m][3].ToString().ToLower() == "" ? "item.gif" : dtChild.Rows[m][3].ToString(), dtChild.Rows[m][4].ToString() + "_" + siteID);
            if (dtChild.Rows[m]["HaveChildNav"].ToString().ToLower() == "true")
            {
                EmptyNum++;
                strRFlaglist.Append(GetChildRoleFlagList(Convert.ToInt32(dtChild.Rows[m][0]), roleFlag, siteID));
            }
        }
        strRFlaglist.Append("</ul>");
        return strRFlaglist.ToString();
    }
    #endregion
}
